package com.epfresh.adapter;

import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.epfresh.R;
import com.epfresh.api.universalimageloader.core.DisplayImageOptions;
import com.epfresh.api.universalimageloader.core.ImageLoader;
import com.epfresh.api.universalimageloader.core.assist.ImageScaleType;
import com.epfresh.bean.DistributionCenterEntity;
import multitype.ItemViewBinder;

/**
 * Author: Chen
 * Date: 2019/1/18
 * Desc:
 */
public class DeliveryCenterViewBinderNew extends ItemViewBinder<DistributionCenterEntity, DeliveryCenterViewBinderNew.ViewHolder> {
    private OnDeliveryCenterClickListener onDeliveryCenterClickListener;

    public void setOnDeliveryCenterClickListener(OnDeliveryCenterClickListener onDeliveryCenterClickListener) {
        this.onDeliveryCenterClickListener = onDeliveryCenterClickListener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View itemView = inflater.inflate(R.layout.layout_delivery_center, parent, false);
        return new ViewHolder(itemView);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull final DistributionCenterEntity item) {
        int size = item.getDistributionBeanList().size();

        holder.txt_center_count.setText("共" + item.getDisMore() + "家");
        if (size == 1) {
            holder.center2.setVisibility(View.INVISIBLE);
            holder.center3.setVisibility(View.INVISIBLE);

            ImageLoader.getInstance().displayImage(item.getDistributionBeanList().get(0).getLogo(), holder.img_center1, options);
            holder.txt_center1_name.setText(item.getDistributionBeanList().get(0).getName());
            holder.txt_good_rating1.setText(item.getDistributionBeanList().get(0).getGoodCommentsRate() + "%");
            holder.txt_good_shop1.setText(item.getDistributionBeanList().get(0).getProductCount() + "");
            if (item.getDistributionBeanList().get(0).getCustomerStatus() == 2) {
                holder.img_vip1.setVisibility(View.VISIBLE);
            } else {
                holder.img_vip1.setVisibility(View.GONE);
            }

            holder.center1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onDeliveryCenterClickListener != null) {
                        if (canClick(item, 0)) {
                            onDeliveryCenterClickListener.OnCenterClick(item, 0, 0);

                        } else if (item.getDistributionBeanList().get(0).getCustomerStatus() == 1) {
                            onDeliveryCenterClickListener.OnCenterClick(item, 0, 1);

                        } else {
                            onDeliveryCenterClickListener.OnCenterClick(item, 0, 2);
                        }
                    }
                }
            });
        }


        if (size == 2) {
            holder.center3.setVisibility(View.INVISIBLE);

            ImageLoader.getInstance().displayImage(item.getDistributionBeanList().get(0).getLogo(), holder.img_center1, options);
            holder.txt_center1_name.setText(item.getDistributionBeanList().get(0).getName());
            holder.txt_good_rating1.setText(item.getDistributionBeanList().get(0).getGoodCommentsRate() + "%");
            holder.txt_good_shop1.setText(item.getDistributionBeanList().get(0).getProductCount() + "");

            if (item.getDistributionBeanList().get(0).getCustomerStatus() == 2) {
                holder.img_vip1.setVisibility(View.VISIBLE);
            } else {
                holder.img_vip1.setVisibility(View.GONE);
            }

            ImageLoader.getInstance().displayImage(item.getDistributionBeanList().get(1).getLogo(), holder.img_center2, options);
            holder.txt_center2_name.setText(item.getDistributionBeanList().get(1).getName());
            holder.txt_good_rating2.setText(item.getDistributionBeanList().get(1).getGoodCommentsRate() + "%");
            holder.txt_good_shop2.setText(item.getDistributionBeanList().get(1).getProductCount() + "");

            if (item.getDistributionBeanList().get(1).getCustomerStatus() == 2) {
                holder.img_vip2.setVisibility(View.VISIBLE);
            } else {
                holder.img_vip2.setVisibility(View.GONE);
            }


            holder.center1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onDeliveryCenterClickListener != null) {
                        if (canClick(item, 0)) {
                            onDeliveryCenterClickListener.OnCenterClick(item, 0, 0);

                        } else if (item.getDistributionBeanList().get(0).getCustomerStatus() == 1) {
                            onDeliveryCenterClickListener.OnCenterClick(item, 0, 1);

                        } else {
                            onDeliveryCenterClickListener.OnCenterClick(item, 0, 2);
                        }
                    }
                }
            });


            holder.center2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onDeliveryCenterClickListener != null) {
                        if (canClick(item, 1)) {
                            onDeliveryCenterClickListener.OnCenterClick(item, 1, 0);

                        } else if (item.getDistributionBeanList().get(1).getCustomerStatus() == 1) {
                            onDeliveryCenterClickListener.OnCenterClick(item, 1, 1);

                        } else {
                            onDeliveryCenterClickListener.OnCenterClick(item, 1, 2);
                        }
                    }
                }
            });
        }


        if (size == 3) {
            holder.center1.setVisibility(View.VISIBLE);
            holder.center2.setVisibility(View.VISIBLE);
            holder.center3.setVisibility(View.VISIBLE);

            ImageLoader.getInstance().displayImage(item.getDistributionBeanList().get(0).getLogo(), holder.img_center1, options);
            holder.txt_center1_name.setText(item.getDistributionBeanList().get(0).getName());
            holder.txt_good_rating1.setText(item.getDistributionBeanList().get(0).getGoodCommentsRate() + "%");
            holder.txt_good_shop1.setText(item.getDistributionBeanList().get(0).getProductCount() + "");

            if (item.getDistributionBeanList().get(0).getCustomerStatus() == 2) {
                holder.img_vip1.setVisibility(View.VISIBLE);
            } else {
                holder.img_vip1.setVisibility(View.GONE);
            }

            ImageLoader.getInstance().displayImage(item.getDistributionBeanList().get(1).getLogo(), holder.img_center2, options);
            holder.txt_center2_name.setText(item.getDistributionBeanList().get(1).getName());
            holder.txt_good_rating2.setText(item.getDistributionBeanList().get(1).getGoodCommentsRate() + "%");
            holder.txt_good_shop2.setText(item.getDistributionBeanList().get(1).getProductCount() + "");

            if (item.getDistributionBeanList().get(1).getCustomerStatus() == 2) {
                holder.img_vip2.setVisibility(View.VISIBLE);
            } else {
                holder.img_vip2.setVisibility(View.GONE);
            }

            ImageLoader.getInstance().displayImage(item.getDistributionBeanList().get(2).getLogo(), holder.img_center3, options);
            holder.txt_center3_name.setText(item.getDistributionBeanList().get(2).getName());
            holder.txt_good_rating3.setText(item.getDistributionBeanList().get(2).getGoodCommentsRate() + "%");
            holder.txt_good_shop3.setText(item.getDistributionBeanList().get(2).getProductCount() + "");

            if (item.getDistributionBeanList().get(2).getCustomerStatus() == 2) {
                holder.img_vip3.setVisibility(View.VISIBLE);
            } else {
                holder.img_vip3.setVisibility(View.GONE);
            }


            holder.center1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onDeliveryCenterClickListener != null) {
                        if (canClick(item, 0)) {
                            onDeliveryCenterClickListener.OnCenterClick(item, 0, 0);

                        } else if (item.getDistributionBeanList().get(0).getCustomerStatus() == 1) {
                            onDeliveryCenterClickListener.OnCenterClick(item, 0, 1);

                        } else {
                            onDeliveryCenterClickListener.OnCenterClick(item, 0, 2);
                        }
                    }
                }
            });


            holder.center2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onDeliveryCenterClickListener != null) {
                        if (canClick(item, 1)) {
                            onDeliveryCenterClickListener.OnCenterClick(item, 1, 0);

                        } else if (item.getDistributionBeanList().get(1).getCustomerStatus() == 1) {
                            onDeliveryCenterClickListener.OnCenterClick(item, 1, 1);

                        } else {
                            onDeliveryCenterClickListener.OnCenterClick(item, 1, 2);
                        }
                    }
                }
            });


            holder.center3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onDeliveryCenterClickListener != null) {
                        if (canClick(item, 2)) {
                            onDeliveryCenterClickListener.OnCenterClick(item, 2, 0);

                        } else if (item.getDistributionBeanList().get(2).getCustomerStatus() == 1) {
                            onDeliveryCenterClickListener.OnCenterClick(item, 2, 1);

                        } else {
                            onDeliveryCenterClickListener.OnCenterClick(item, 2, 2);
                        }
                    }
                }
            });
        }

        holder.ll_more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onDeliveryCenterClickListener != null) {
                    onDeliveryCenterClickListener.OnMoreCenterClick();
                }
            }
        });

    }

    private boolean canClick(DistributionCenterEntity item, int pos) {
        boolean b1 = item.getDistributionBeanList().get(pos).getIsPublicOffer() == 1;
        boolean b2 = item.getDistributionBeanList().get(pos).getIsPublicOffer() == 0 && item.getDistributionBeanList().get(pos).getCustomerStatus() == 2;
        return b1 || b2;

    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private RelativeLayout center1, center2, center3;
        private LinearLayout ll_more;
        private TextView txt_center_count;

        private ImageView img_center1;
        private TextView txt_center1_name, txt_good_rating1, txt_good_shop1;

        private ImageView img_center2;
        private TextView txt_center2_name, txt_good_rating2, txt_good_shop2;

        private ImageView img_center3;
        private TextView txt_center3_name, txt_good_rating3, txt_good_shop3;

        private ImageView img_vip1, img_vip2, img_vip3;


        ViewHolder(View itemView) {
            super(itemView);

            center1 = itemView.findViewById(R.id.center1);
            center2 = itemView.findViewById(R.id.center2);
            center3 = itemView.findViewById(R.id.center3);

            ll_more = itemView.findViewById(R.id.ll_more);
            txt_center_count = itemView.findViewById(R.id.txt_center_count);

            img_center1 = itemView.findViewById(R.id.img_center1);
            txt_center1_name = itemView.findViewById(R.id.txt_center1_name);
            txt_good_rating1 = itemView.findViewById(R.id.txt_good_rating1);
            txt_good_shop1 = itemView.findViewById(R.id.txt_good_shop1);

            img_center2 = itemView.findViewById(R.id.img_center2);
            txt_center2_name = itemView.findViewById(R.id.txt_center2_name);
            txt_good_rating2 = itemView.findViewById(R.id.txt_good_rating2);
            txt_good_shop2 = itemView.findViewById(R.id.txt_good_shop2);

            img_center3 = itemView.findViewById(R.id.img_center3);
            txt_center3_name = itemView.findViewById(R.id.txt_center3_name);
            txt_good_rating3 = itemView.findViewById(R.id.txt_good_rating3);
            txt_good_shop3 = itemView.findViewById(R.id.txt_good_shop3);

            img_vip1 = itemView.findViewById(R.id.img_vip1);
            img_vip2 = itemView.findViewById(R.id.img_vip2);
            img_vip3 = itemView.findViewById(R.id.img_vip3);

        }
    }

    private DisplayImageOptions options = new DisplayImageOptions.Builder()
            .showImageOnLoading(R.mipmap.icon_fail_merchant) //设置图片在下载期间显示的图片
            .showImageForEmptyUri(R.mipmap.icon_fail_merchant)//设置图片Uri为空或是错误的时候显示的图片
            .showImageOnFail(R.mipmap.icon_fail_merchant)
            .cacheInMemory(true)// 设置下载的图片是否缓存在内存中
            .cacheOnDisk(true)// 设置下载的图片是否缓存在SD卡中
            .considerExifParams(true) // 是否考虑JPEG图像EXIF参数（旋转，翻转）
            .imageScaleType(ImageScaleType.EXACTLY_STRETCHED)// 设置图片以如何的编码方式显示
            .bitmapConfig(Bitmap.Config.RGB_565)
            .build();


    public interface OnDeliveryCenterClickListener {
        void OnMoreCenterClick();

        void OnCenterClick(DistributionCenterEntity item, int centerPos, int type);//type：0：会员；1：申请中；2：申请会员
    }

}
